package com.iot08.api.service;

import com.iot08.api.constant.Constant;
import com.iot08.common.entity.app.BloodPressureRecord;
import com.iot08.common.entity.app.HeartRateRecord;
import com.iot08.common.service.base.BaseService;
import com.iot08.common.utils.DateUtil;
import com.iot08.common.utils.response.ResponseEnum;
import com.iot08.common.utils.response.ResponseMap;
import com.mongodb.client.FindIterable;
import com.mongodb.client.model.Filters;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.bson.Document;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @version 1.0
 * @Description:
 * @author: Rock
 * @time: 2021/3/8 18:33
 */
@Slf4j
@Service
public class BloodPressureRecordService extends BaseService<BloodPressureRecord> {

    private int error_0 = 0;
    private int error_1 = 1;

    /**
     * @param imei
     * @param value
     * @time: 2021/3/9 16:39
     * @author: Rock
     * @version 1.0
     */
    public BloodPressureRecord add(String imei, String value, long dateTime) {
        BloodPressureRecord record = new BloodPressureRecord();
        record.setImei(imei);
        record.setValue(value);
        record.setDateTime(dateTime);
        record.setUpDayStr(DateUtil.dateToStr(new Date(), DateUtil.FORMAT_4));
        record.setExpiryAt(DateUtil.getSystemExpiryAt());
        return this.save(record);
    }

    /**
     * @param imei
     * @param upDayStr
     * @time: 2021/3/8 19:27
     * @author: Rock
     * @version 1.0
     */
    public List<Object> listData(String imei, String upDayStr) {
        List<Object> list = new ArrayList<>();
        Query query = new Query(Criteria.where("i").is(imei).and("upDayStr").is(upDayStr));
        List<BloodPressureRecord> records = this.find(query.with(Sort.by(Sort.Order.desc("dateTime"))));
        for (BloodPressureRecord bloodPressureRecord : records) {
            Map<String, Object> temp = new HashMap<>();
            temp.put("time", bloodPressureRecord.getDateTime());
            temp.put("value", bloodPressureRecord.getValue());
            list.add(temp);
        }
        return list;
    }

    /**
     * @param imei
     * @time: 2021/3/15 18:54
     * @author: Rock
     * @version 1.0
     */
    public Object listByImei(String imei, String timestamp, String authKey, String vendorCode) {
        log.info("imei={}. timestamp={}. authKey={}. vendorCode={}", imei, timestamp, authKey, vendorCode);
        Map<String, Object> res = new HashMap<>();
        String key = DigestUtils.md5Hex(Constant.AUTH_KEY + timestamp).toUpperCase();
        if (!key.equals(authKey.toUpperCase())) {
            res.put("error", error_1);
            res.put("msg", "认证失败.Key值错误");
            return res;
        }
        List<Object> list = new ArrayList<>();
        Document query = new Document();
        query.put("i", imei);
        FindIterable<Document> records = this.findDoc(query, Filters.eq("dateTime", -1), 0, -1);
        for (Document doc : records) {
            Map<String, Object> temp = new HashMap<>();
            temp.put("id", doc.get("_id").toString());
            temp.put("time", doc.get("dateTime"));
            temp.put("value", doc.get("value"));
            list.add(temp);
        }
        res.put("dataList", list);
        res.put("error", error_0);
        res.put("msg", "");
        res.put("deviceNumber", imei);
        return res;
    }

    /**
     * @param imei
     * @param value
     * @param dateTime
     * @time: 2021/3/16 10:20
     * @author: Rock
     * @version 1.0
     */
    public BloodPressureRecord get(String imei, String value, long dateTime) {
        Query query = new Query(Criteria.where("imei").is(imei).and("value").is(value).and("dateTime").is(dateTime));
        return this.findOne(query);
    }

}
